const db = require('../config/DBconnector');

/**
 * Base Service
 */
class BaseService {
  /**
   * constructor
   * @param {BaseDao<T>} dao instance
   */
  constructor(dao) {
    this.dao = dao;
  }

  /**
   * create a recode
   * @param {Model} record
   * @return {Model} record
   */
  create(record) {
    return this.dao.create(record);
  }

  /**
   * find by id
   * @param {Integer} id
   * @return {Model} record
   */
  findById(id) {
    return this.dao.findById(id);
  }

  /**
   * find all
   * @param {Object<where>} where
   * @param {Array<include>} include
   * @param {Integer} limit
   * @param {Integer} offset default 0
   * @return {Array<Model>} records
   */
  findAll(where, include, limit, offset) {
    return this.dao.findAll(where, include, limit, offset);
  }

  /**
   * find one record
   * @param {Object<where>} where
   * @param {Array<include>} include
   * @param {Integer} limit default 1
   * @param {Integer} offset default 0
   * @return {Model} record
   */
  findOne(where, include, limit, offset) {
    return this.dao.findOne(where, include, limit, offset);
  }

  /**
   * transaction wrapper
   * @param {Function} fn(transactionId)
   * @return {Promise}
   */
  transaction(fn) {
    return db.transaction(fn);
  }
}

module.exports = BaseService;
